package com.zero.rocketmq.api.base.customer;

import com.zero.rocketmq.api.utils.RocketMQContents;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;

/**
 * 推模式消费者
 */
public class Consumer {

    public static void main(String[] args) throws MQClientException {

        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(RocketMQContents.CUSTOMER_GROUP);

        consumer.setNamesrvAddr(RocketMQContents.NAMESERVER_ADDR);

        try {
            consumer.subscribe(RocketMQContents.TOPIC,"*");
            //1.根据时间过滤
            //2.默认回溯消耗时间半小时前
            consumer.setConsumeTimestamp("20211116135600");
            consumer.registerMessageListener(new MessageListenerConcurrently() {
                @Override
                public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
                    System.out.printf("%s Receive New Messages: %s %n", Thread.currentThread().getName(), msgs);
                    return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
                }
            });
        }catch (Exception e){


        }
        consumer.start();
        System.out.printf("Consumer Started.%n");

    }
}
